home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
205_01
/
bg.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
18KB
|
774 lines
/*
HEADER: CUG205.00;
TITLE: BG.C - Backgammon II (for MS-DOS);
VERSION: 2.00;
DATE: 10/14/86;
DESCRIPTION: "Leor Zolman's public domain backgammon
for PC/MS-DOS. Uses ANSI.SYS for screen control and colors.";
KEYWORDS: games, backgammon;
SYSTEM: MS-DOS;
FILENAME: BG.C.;
WARNINGS: "This program uses the ANSI color standard. Include the
following line in your CONFIG.SYS file at system start.
device = ansi.sys
For further details on ANSI.SYS, refer to your DOS manual.
The author claims the copyright to the MS-DOS version and
authorizes non-commercial use only.";
SEE-ALSO: CUG102, BGTALK.C, BGDISP.C;
AUTHORS: Michael Yokoyama;
COMPILERS: Microsoft v.3.00.;
*/
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bgdisp.c" /* Display subroutines */
#include "bgtalk.c" /* Conversation subroutines */
#define LINT_ARGS 1 /* Enable strong lint-type checking */
#define NIL (-1)
#define NO 0
#define YES 1
#define MAXGMOV 10
#define MAXLINE 150
#define MAXIMOVES 1000
/* Declare all functions which do not return integers */
char strips();
char c, level;
int i, j, l, m, count;
int die1, die2; /* Dice */
int mwin, ywin; /* Rolling count of my wins and your wins */
int red[31]; /* Array of the board's red pieces */
int white[31]; /* Array of the board's white pieces */
int prob[13]; /* Probability? */
int goodmove[MAXGMOV];
int probmove[MAXGMOV];
int imoves;
struct {
int pos[4];
int mov[4];
} moves[MAXIMOVES];
main()
{
int a, i, k, n, t, go[6];
int firstmove; /* Flag == true on first move only */
int wrongmove; /* Flag == true if move is incorrect */
long iths; /* Hundredths of secs for random seed */
char s[MAXLINE];
mwin = ywin = 0; /* Reset game score */
time(&iths); /* Seed the random generator */
srand(iths);
title();
printf ("Do you want the instructions?\n");
if ((c = getche()) == 'y')
help();
header();
select();
newgame:
init();
firstmove = 1;
go[5] = NIL;
message0();
throw:
roll();
printf ("My roll is: %c[37m%d%c[30m, your roll is: %c[31m%d%c[30m\n",27,die1,27,27,die2,27);
if (die1 == die2) {
printf ("Tied up, roll again:\n");
sleep();
goto throw;
}
printf ("\n%s will go first . . .\n\n", die1 > die2 ? "I" : "You");
printf ("Press any key:\n");
getch();
cls();
if (die2 > die1)
goto red;
white:
if (!firstmove)
roll(); /* Skip roll on first move */
firstmove = 0; /* First roll not true anymore */
order();
show2();
if (!canmove(white,red)) {
message6();
goto red;
}
message5();
if (nextmove(white,red) == NIL)
goto red;
if (piececount(white,0,24) == 0) {
sleep();
cls();
printf ("\7I won!\n\n");
++mwin;
score();
if (mwin == ywin)
printf ("All tied up! Who's gonna pull ahead?\n");
else if (mwin > 2 && !ywin)
printf ("It's about time you managed to win a game!\n");
else message1();
goto again;
}
red:
dboard();
if (!firstmove)
roll(); /* Skip roll on first move */
firstmove = 0; /* First roll not true anymore */
order();
show1();
if (!canmove(red,white)) {
message7();
getch();
goto white;
}
retry:
message3();
while (a = kbhit() != 0) {
time(&iths);
srand(iths); /* Exercise the random generator while */
} /* waiting for a move to be made . . . */
gets(s);
if (strips(s) == '?' || strips(s) == 'h') {
helpscrn();
printf ("\nDo you want to see all the instructions?\n");
if ((c = getche()) == 'y')
help();
cls();
dboard();
show1();
wrongmove = YES;
}
if (strips(s) == 'q') { /* check for quit command */
printf ("Aborting game: Enter Y to confirm\n");
if ((c = getche()) == 'y')
goto quit;
dboard();
show1();
wrongmove = YES;
}
if (strips(s) == 'b') { /* check for print board command */
cls();
dboard();
show1();
wrongmove = YES;
}
if (strips(s) != '-' && !isdigit(strips(s))) {
cls();
dboard();
printf ("\7You must move. ");
show1();
wrongmove = YES;
}
/* convert commas to spaces */
for (i = 0; s[i]; i++)
if (s[i] == ',')
s[i] = ' ';
n = sscanf(s,"%d%d%d%d%d",&go[0],&go[1],&go[2],&go[3],&go[4]);
if ((die1 != die2 && n > 2) || n > 4) {
cls();
dboard();
printf ("\7Too many moves. ");
show1();
wrongmove = YES;
}
if (((die1 != die2) && n < 2) || ((die1 == die2) && n < 4)) {
if (n == redcount())
goto moveok;
printf ("\7Ok to skip move(s)?");
if ((c = getche()) != 'y') {
cls();
dboard();
printf ("\7Enter your moves again: ");
show1();
wrongmove = YES;
}
printf ("\n");
}
moveok:
go[n] = NIL;
if (*s == '-') {
go[0] = -go[0];
t = die1;
die1 = die2;
die2 = t;
}
for (k = 0; k<n; k++) {
if (0 <= go[k] && go[k] <= 24)
continue;
else {
cls();
dboard();
printf ("\7You can't move %d. ",go[k]);
show1();
wrongmove = YES;
}
}
if (play(red,white,go))
wrongmove = YES;
if (wrongmove == YES) {
wrongmove = NO;
goto retry;
}
if (piececount(red,0,24) == 0) {
sleep();
cls();
printf ("\7You win . . .\n");
++ywin;
score();
if (mwin == ywin)
printf ("\nLooks like now we're dead even . . . it's tiebreaker time!\n");
else if (ywin > 2 && !mwin)
printf ("\nYou're lucky I don't throw the dice. . . .\n");
else message2();
again:
message4();
poll:
c = getch();
if (c == 'y') {
cls();
goto newgame;
}
else if (c != 'n') {
printf("\7");
goto poll;
}
else goto quit;
}
goto white;
quit:
cls();
exit(1);
}
/* Initialize the board and the probabilities */
init()
{
int i;
/* Clear off board and enter the starting positions of the pieces */
for (i = 0; i <= 30; i++)
red[i] = white[i] = 0;
red[1] = white[1] = 2;
red[12] = white[12] = 5;
red[17] = white[17] = 3;
red[19] = white[19] = 5;
for (i = 0; i <= 30; i++) /* Note: Is this code necessary? */
prob[i] = 0;
prob[0] = 0;
prob[1] = 11;
prob[2] = 12;
prob[3] = 13;
prob[4] = 14;
prob[5] = 15;
prob[6] = 16;
prob[7] = 6;
prob[8] = 5;
prob[9] = 4;
prob[10] = 3;
prob[11] = 2;
prob[12] = 1;
}
/* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
/* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
/* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
/* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
/* STRATEGY SECTION.STRATEGY SECTION.STRATEGY SECTION.*/
int play(player,opponent,pos)
int *player,*opponent,pos[];
{
int k,n,die,ipos;
/* Blots on player[0] must be moved first */
for (k = 0;k<player[0];k++) {
if (pos[k] == NIL)
break;
if (pos[k] != 0) {
printf ("\nPlay your BAR piece (on point 0) first\n");
return(-1);
}
}
for (k = 0;(ipos = pos[k]) != NIL;k++) {
die = k?die2:die1;
n = 25-ipos-die;
if (player[ipos] == 0)
goto illegal;
if (n>0 && opponent[n] >= 2)
goto illegal;
if (n <= 0) {
if (piececount(player,0,18) != 0)
goto illegal;
if ((ipos+die) != 25 && piecec